package com.nami.gate.dubbo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.nami.gate.api.request.login.BindingRequest;
import com.nami.gate.api.request.login.LogoutRequest;
import com.nami.gate.api.request.login.TempChannelCloseRequest;
import com.nami.gate.api.request.pong.PongRequest;
import com.nami.gate.api.service.RemoteLoginService;
import com.nami.gate.cache.LocalChannelCache;
import com.nami.gate.cache.TempChannelCache;
import com.nami.gate.constants.ChannelConstants;
import com.nami.gate.utils.JsonUtils;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

/**
 * 描述：
 *
 * @Author: lbc
 * @Date: 2023-07-16 18:35
 * @email: 594599620@qq.com
 * @Description: keep coding
 */
@Slf4j
@DubboService
@Service
@Deprecated
public class RemoteLoginServiceImpl implements RemoteLoginService {

    @Override
    public void loginSuccess(BindingRequest request) {
        TimeInterval timer = DateUtil.timer();
        log.info("[login] ws节点开始处理登录成功消息, 参数:{}", JsonUtils.toJsonString(request));
        String tempNo = request.getTempFingerprint();
        Channel channel = TempChannelCache.getTempChannel(tempNo);
        if (channel == null) {
            log.warn("tempChannel is null。");
            TempChannelCache.removeChannel(tempNo);
            return;
        }
        bindingUserInfoToChannel(channel, request);
        LocalChannelCache.put(channel.id().asLongText(), request.getIdentityNo());
        LocalChannelCache.put(request.getIdentityNo(), channel);
        // 删除临时cache
        TempChannelCache.removeChannel(tempNo);
        channel.writeAndFlush(request.getPacket());
        log.info("[login] ws节点向客户端【{}】写入成功. use:{}ms", channel.remoteAddress(), timer.interval());
    }


    @Override
    public void loginout(LogoutRequest dto) {
        try {
            Channel channel = LocalChannelCache.get(dto.getIdentityNo());
            channel.writeAndFlush(dto.getPacket());
            // 删除L1Cache
            LocalChannelCache.removeAll(dto.getIdentityNo());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[loginout] 注销出现错误：{}", dto.getIdentityNo());
        }
    }

    @Override
    public void closeChannel(String identityNo) {
        try {
            LocalChannelCache.removeAll(identityNo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("[nami-channel] 系统关闭用户【{}】挂载信息, 出现错误, 或已被移除.", identityNo);
        }
    }

    @Override
    public void tempChannelClose(TempChannelCloseRequest dto) {
        try {
            Channel tempChannel = TempChannelCache.getTempChannel(dto.getTempFingerprint());
            if (tempChannel == null) {
                TempChannelCache.removeChannel(dto.getTempFingerprint());
            }
            tempChannel.writeAndFlush(dto.getPacket());
            TempChannelCache.removeChannel(dto.getTempFingerprint());
            tempChannel.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void pong(PongRequest dto) {

    }

    private static void bindingUserInfoToChannel(Channel channel, BindingRequest wrap) {
        channel.attr(AttributeKey.valueOf(ChannelConstants.USER_TOKEN)).set(wrap.getToken());
        channel.attr(AttributeKey.valueOf(ChannelConstants.USER_ID)).set(wrap.getUserId());
        channel.attr(AttributeKey.valueOf(ChannelConstants.APP_KEY)).set(wrap.getAppKey());
        channel.attr(AttributeKey.valueOf(ChannelConstants.PLATFORM)).set(wrap.getPlatform());
        channel.attr(AttributeKey.valueOf(ChannelConstants.IMEI)).set(wrap.getImei());
        channel.attr(AttributeKey.valueOf(ChannelConstants.READ_TIME)).set(System.currentTimeMillis());
    }

}
